#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QDesktopWidget>
#include <QPainter>
#include <QGraphicsItem>
#include <QtCore>
#include <QtGui>
#include <QIcon>

#include "mainview.h"
#include "icon.h"

MainView::MainView(QGraphicsView *parent)
    : QGraphicsView(parent)
{
    view = new QGraphicsView();
    scene = new QGraphicsScene(0,0,QApplication::desktop()->width(),QApplication::desktop()->height());

    img_path = new QString(QApplication::applicationDirPath());

    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    image = new QImage(QString("%1/Images/background_grid.png").arg(*img_path));

    QImage lampada_on, lampada_off, lampada_on2, lampada_off2;

    lampada_on.load(QString("%1/Images/lampada_on.png").arg(*img_path));
    lampada_off.load(QString("%1/Images/lampada_off.png").arg(*img_path));
    lampada_on2.load(QString("%1/Images/lampada_on2.png").arg(*img_path));
    lampada_off2.load(QString("%1/Images/lampada_off2.png").arg(*img_path));

    Icon *lampada_1 = new Icon(lampada_on2, lampada_off2, scene);
    Icon *lampada_2 = new Icon(lampada_on2, lampada_off2, scene);
    Icon *lampada_3 = new Icon(lampada_on2, lampada_off2, scene);

    scene->addItem(lampada_1);
    scene->addItem(lampada_2);
    scene->addItem(lampada_3);
    setScene(scene);

  //  setViewportMargins(-3,-5,-2,-2);

    lampada_1->setPos(700,500);
    lampada_2->setPos(750,500);
    lampada_3->setPos(800,500);

    view->setBackgroundBrush(*image);
}

void MainView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);
    fitInView(QRectF(0,0,scene->width(),scene->height()), Qt::IgnoreAspectRatio);
    scene->setSceneRect(0,0,QApplication::desktop()->width(),QApplication::desktop()->height());
    scene->update();
}

void MainView::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Escape) {
           if(this->isFullScreen())
               this->showMaximized();
           else this->showFullScreen();
    }
}

void MainView::drawBackground(QPainter *painter, const QRectF &rect)
{
    painter->setRenderHint(QPainter::SmoothPixmapTransform);
    QRectF r;
    r.setRect(-2,-2,scene->width()+5,scene->height()+5);
}
